//
//  UserAccount.m
//  THCustomer
//
//  Created by lichentao on 13-8-12.
//  Copyright (c) 2013年 efuture. All rights reserved.
//


#import "UserAccount.h"

@implementation UserAccount

+ (UserAccount *)instance {
    static UserAccount *_obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _obj = [[UserAccount alloc] init];
    });
    return _obj;
}

- (NSString *)savePath {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *preferencePath = [pathArray objectAtIndex:0];
    NSString *savePath = [preferencePath stringByAppendingString:@"/Account.plist"];
    return savePath;
}

- (id)init {
    self = [super init];
    if (self) {
        NSString *savePath = [self savePath];
        NSFileManager *df = [NSFileManager defaultManager];
        if ([df fileExistsAtPath:savePath]) {
            NSDictionary *accountInfo = [NSDictionary dictionaryWithContentsOfFile:savePath];
            [self restoreAccount:accountInfo];
        }
    }
    return self;
}

- (void)restoreAccount:(NSDictionary *)accountInfo{
    if ([accountInfo objectForKey:@"user_id"]) {
        self.user_id = [accountInfo objectForKey:@"user_id"];
    }
    if ([accountInfo objectForKey:@"mobile"]) {
        self.mobile = [accountInfo objectForKey:@"mobile"];
    }
    if ([accountInfo objectForKey:@"img"]) {
        self.user_icon = [accountInfo objectForKey:@"img"];
    }
    if ([accountInfo objectForKey:@"useremail"]) {
        self.email = [accountInfo objectForKey:@"useremail"];
    }
    if ([accountInfo objectForKey:@"username"]) {
        self.user_name = [accountInfo objectForKey:@"username"];
    }
    if ([accountInfo objectForKey:@"true_name"]) {
        self.true_name = [accountInfo objectForKey:@"true_namev"];
    }
    if ([accountInfo objectForKey:@"gender"]) {
        self.gender = [accountInfo objectForKey:@"gender"];
    }
    if ([accountInfo objectForKey:@"intro"]) {
        self.intro = [accountInfo objectForKey:@"intro"];
    }
    if ([accountInfo objectForKey:@"sign"]) {
        self.session_id = [NSString stringWithFormat:@"%@",[accountInfo objectForKey:@"sign"]];
    }

}

- (void)updateAccount:(NSDictionary *)accountInfo {
    [self restoreAccount:accountInfo];
    [self saveAccount];
}

- (BOOL)isLogin {
    if (self.session_id && (self.session_id.length > 0)) {
        return YES;
    }
    return NO;
}

- (void)logout {
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"useremail"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"password"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    self.session_id = nil;
    self.true_name = nil;
    self.user_id = nil;
    self.user_name = nil;
    self.mobile = nil;
    self.gender = nil;
    self.intro = nil;
    self.email = nil;
    self.user_icon = nil;
    NSString *savePath = [self savePath];
    NSFileManager *df = [NSFileManager defaultManager];
    if ([df fileExistsAtPath:savePath]) {
        [df removeItemAtPath:savePath error:nil];
    }
}

- (void)saveAccount {
    NSString *savePath = [self savePath];
    NSMutableDictionary *saveAccount = [NSMutableDictionary dictionary];

    if (self.model) {
        [saveAccount setValue:[NSNumber numberWithBool:self.model] forKey:@"model"];
    }
    if (self.user_id) {
        [saveAccount setValue:self.user_id forKey:@"user_id"];
    }
    if (self.mobile) {
        [saveAccount setValue:self.mobile forKey:@"mobile"];
    }
    if (self.email) {
        [saveAccount setValue:self.email forKey:@"email"];
    }
    if (self.user_name) {
        [saveAccount setValue:self.user_name forKey:@"user_name"];
    }
    if (self.true_name) {
        [saveAccount setValue:self.true_name forKey:@"true_name"];
    }
    if (self.gender) {
        [saveAccount setValue:self.gender forKey:@"gender"];
    }
    if (self.intro) {
        [saveAccount setValue:self.intro forKey:@"intro"];
    }

    if (self.user_icon) {
        [saveAccount setValue:self.user_icon forKey:@"photo_url"];
    }
    if (self.session_id) {
        [saveAccount setValue:self.session_id forKey:@"session_id"];
    }

    [saveAccount writeToFile:savePath atomically:YES];
}

@end

//----------------------------------jsonModel方式处理用户数据
@implementation UserAccountModel
@end

@implementation UpLoadImgModel
@end

